package com.css.fxfzypg.modules.ScheduleTask;

import com.css.fxfzypg.manage.lethalitydifference.service.LethalityDifferenceService;
import com.css.fxfzypg.modules.SettingSeismic.entity.SeismicIntensityRelation;
import com.css.fxfzypg.modules.SettingSeismic.repository.IntensityRelationRepository;
import com.css.fxfzypg.modules.baseData.entity.BuildingVulnerability;
import com.css.fxfzypg.modules.baseData.service.BasicDataService;
import com.css.fxfzypg.modules.baseData.service.BuildingVulnerabilityService;
import com.css.fxfzypg.modules.earthquakedisasterloss.entity.SettingSeismicInfluenceflied;
import com.css.fxfzypg.modules.earthquakedisasterloss.repository.SettingSeismicInfluencefliedRepository;
import com.css.fxfzypg.modules.earthquakedisasterloss.service.YpgResultPbeService;
import com.css.fxfzypg.modules.model.entity.DataPbeInjuries;
import com.css.fxfzypg.modules.model.entity.DataPbeIntStrArea;
import com.css.fxfzypg.modules.model.entity.DataPbeLosses;
import com.css.fxfzypg.modules.model.service.CasualtyService;
import com.css.fxfzypg.modules.model.service.EconomicLossesService;
import com.css.fxfzypg.modules.preAssessResult.entity.SeiCapDistribution;
import com.css.fxfzypg.modules.preAssessResult.service.PreAssessResultService;
import com.css.fxfzypg.modules.preassesstaskmanage.entity.PreAssessTaskEntity;
import com.css.fxfzypg.modules.preassesstaskmanage.service.PreAssessTaskService;
import com.css.fxfzypg.modules.resultupload.repository.ResultTotalRepository;
import com.css.fxfzypg.modules.resultupload.repository.entity.ResultMainEntity;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description: fxfzypg
 * @Author: lhl
 * @CreateDate: 2023/6/1 16:27
 * 临时的定时任务
 */
@RequestMapping("/temporary")
@RestController
public class TemporarySchTask {
    @Resource
    BuildingVulnerabilityService buildingVulnerabilityService;
    @Resource
    SysAreaService sysAreaService;
    @Resource
    IntensityRelationRepository intensityRelationRepository;
    @Resource
    SettingSeismicInfluencefliedRepository settingSeismicInfluencefliedRepository;
    @Resource
    CasualtyService casualtyService;
    @Resource
    EconomicLossesService economicLossesService;
    @Resource
    PreAssessResultService preAssessResultService;
    @Resource
    ResultTotalRepository resultTotalRepository;
    @Resource
    LethalityDifferenceService lethalityDifferenceService;

    @RequestMapping("/schTask")
    @Scheduled(cron ="0 05 15 05 06 ?")
    public void schTask(){
        try {
            schTask1();
            schTask2();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public void schTask1() throws Exception {
        List<SeismicIntensityRelation> all = intensityRelationRepository.getAll();
        for (SeismicIntensityRelation byTaskId : all) {
            String taskId=byTaskId.getTaskId();
            // SeismicIntensityRelation byTaskId = intensityRelationRepository.getByTaskId(taskId);
            if (PlatformObjectUtils.isEmpty(byTaskId.getButton1_status1())) {
                SettingSeismicInfluenceflied influenceflied =
                        settingSeismicInfluencefliedRepository.findByTaskIdLimit(taskId);
                if (influenceflied!=null) {
                    byTaskId.setButton1_status1("1");
                }
            }

            DataPbeIntStrArea pbeIntStrArea = casualtyService.findByTaskIdLimit(taskId);
            if (pbeIntStrArea!=null) {
                byTaskId.setButton2_status1("1");
            }
            DataPbeInjuries taskPbe = casualtyService.findByTaskIdLimit2(taskId);
            if (taskPbe!=null) {
                byTaskId.setButton2_status2("1");
            }

            DataPbeLosses dataPbeLosses = economicLossesService.findByTaskIdLimit(taskId);
            if (dataPbeLosses!=null) {
                byTaskId.setButton3_status1("1");
            }

            ResultMainEntity entity = resultTotalRepository.findByTaskIdLimit(taskId);
            if (entity!=null) {
                if (PlatformObjectUtils.isNotEmpty(entity.getInjuredNumBefore())) {
                    byTaskId.setButton2_status3("1");
                }
                if (PlatformObjectUtils.isNotEmpty(entity.getDisasterLossBefore())) {
                    byTaskId.setButton3_status2("1");
                }
                if (PlatformObjectUtils.isNotEmpty(entity.getResettledNumBefore())) {
                    byTaskId.setButton4_status1("1");
                }
                if (PlatformObjectUtils.isNotEmpty(entity.getRescueForceBefore())) {
                    byTaskId.setButton5_status1("1");
                }
                if (PlatformObjectUtils.isNotEmpty(entity.getTentNumBefore())) {
                    byTaskId.setButton6_status1("1");
                }
            }

            if ("1".equals(byTaskId.getModel7Type())) {
                byTaskId.setButton7_status1("1");
            }

            //抗震能力分布
            SeiCapDistribution seiCapCalculation1 = preAssessResultService.findByTaskIdLimit(taskId, "0");
            if (seiCapCalculation1!=null) {
                byTaskId.setButton8_status1("1");
            }
            SeiCapDistribution seiCapCalculation2 = preAssessResultService.findByTaskIdLimit(taskId, "1");
            if (seiCapCalculation2!=null) {
                byTaskId.setButton8_status2("1");
            }
            SeiCapDistribution seiCapCalculation3 = preAssessResultService.findByTaskIdLimit(taskId, "2");
            if (seiCapCalculation3!=null) {
                byTaskId.setButton8_status3("1");
            }

            //区县致死性差异性分析
            String flag = lethalityDifferenceService.getCreateFlag(taskId);
            if ("1".equals(flag)) {
                byTaskId.setButton9_status1("1");
            }

            intensityRelationRepository.updateSeismicIntensityRelation(byTaskId);
        }
    }

    public void schTask2() throws Exception {
        String province="",city="",county="";
        String divisionIds = "";
        List<BuildingVulnerability> vulnerability = buildingVulnerabilityService.getVulnerability();
        for (BuildingVulnerability v: vulnerability){
            if (!v.getProvince().equals(province)||!v.getCity().equals(city)||!v.getPrefecture().equals(county)){
                try {
                    //判断省市县的名称是否符合规范
                    divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(v.getProvince(), "utf-8"),
                            URLEncoder.encode(v.getCity(), "utf-8"),
                            URLEncoder.encode(v.getPrefecture(), "utf-8"));
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    continue;
                }
            }
            String[] split = divisionIds.split("_");
            v.setProvinceId(split[0]);
            v.setCityId(split[1]);
            v.setPrefectureId(split[2]);

            province=v.getProvince();
            city=v.getCity();
            county=v.getPrefecture();
        }
        buildingVulnerabilityService.update(vulnerability);
    }
}
